package com.example.myapplication.ui.calendar;

import androidx.lifecycle.ViewModelProvider;

import android.annotation.SuppressLint;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.example.myapplication.CalendarView;
import com.example.myapplication.R;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

public class CalendarFragment extends Fragment {
    private CalendarView calendarView;
    private TextView monthYearText;
    private Button prevMonthButton;
    private Button nextMonthButton;

    private SimpleDateFormat monthYearFormat = new SimpleDateFormat("yyyy年 MM月", Locale.getDefault());

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_calendar, container, false);

        calendarView = view.findViewById(R.id.calendar_view);
        monthYearText = view.findViewById(R.id.month_year_text);
        prevMonthButton = view.findViewById(R.id.prev_month_button);
        nextMonthButton = view.findViewById(R.id.next_month_button);

        updateMonthYearText(calendarView.getCurrentDate());

        prevMonthButton.setOnClickListener(v -> {
            calendarView.goToPreviousMonth();
            updateMonthYearText(calendarView.getCurrentDate());
        });

        nextMonthButton.setOnClickListener(v -> {
            calendarView.goToNextMonth();
            updateMonthYearText(calendarView.getCurrentDate());
        });

        return view;
    }

    private void updateMonthYearText(Calendar calendar) {
        monthYearText.setText(monthYearFormat.format(calendar.getTime()));
    }
}